﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace Policy_server
{
    class MessengerConnection
    {
        private TcpClient client;
       // private Socket client;
        private byte[] message;//передача, прием сообщения
        private string Name_client;
        private MessangerServer myserver_mess;


        public string NAME_cli
        {
            get { return Name_client; }
            set { Name_client = value; }
        }

        public MessengerConnection(TcpClient cln,string name, MessangerServer svr)
        {
            message = new byte[5000];
            client = cln;
            Name_client = name;
            myserver_mess = svr;
        }
        public void Start()
        {
            try
            {
               // Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                                    
              // client = new TcpClient();
               // socket = client.Client;
               client.Client.BeginReceive(message, 0, message.Length,SocketFlags.None, new AsyncCallback(OnDataReceived), null);

                Console.WriteLine("{0}",client.Connected);
                
               // client.BeginReceive(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null);
                
            
            }
            catch (SocketException se)
            {
                Console.WriteLine(se.Message);
            }
        }

        public void Close()
        {
            client.Close();
        }

        public void OnDataReceived(IAsyncResult asyn)
        {
            try
            {
                int bytesRead = client.Client.EndReceive(asyn);
                if (bytesRead > 0)
                {
                    //отправка информации всем подключенным клиентам
                    myserver_mess.DeliverMessage(message, bytesRead,this);

                    //начало нового прослушивания
                    client.Client.BeginReceive(message, 0, message.Length,SocketFlags.None,new AsyncCallback(OnDataReceived),null);
                   
                }
                else///////////////////////////////////////////////////////////////////////////////////////////////////////
                {
                    myserver_mess.disscon(this);
                }
            }
            catch (Exception err)////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            {//в случае закрытия окна браузера
                myserver_mess.disscon(this);
            }
        }
        public void ReceiveMessage(byte[] data, int byteRead)
        {
            //отпрвка сообщения клиенту
            client.GetStream().Write(data, 0, byteRead);
        }
       

    }
}
